<template>
  <div class="showDialog">
    <el-dialog
      @closed="closeDialog"
      :title="mytitle"
      :visible="showDialog"
      close-on-press-escape
    >
      <el-form
        ref="form_add"
        :model="formData"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item label="部门编号" prop="code">
          <el-input v-model="formData.code"></el-input>
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="formData.manager" placeholder="请选择负责人">
            <el-option
              v-for="item in managerList"
              :key="item.id"
              :label="item.username"
              :value="item.username"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="formData.introduce"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button @click="submit" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getSimpleUserApi } from '@/api/employees'
import {
  addDepartmentApi,
  queryDepartmentApi,
  editDepartmentApi
} from '@/api/department'
export default {
  props: ['showDialog', 'currentPart', 'allDepart'],
  data() {
    return {
      formData: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      // 配置规则
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '长度为1~50个字符', trigger: 'blur' },
          {
            validator: (rules, value, callback) => {
              let flag
              if (this.formData.id) {
                // 编辑部门弹框条件
                flag = this.allDepart
                  .filter(item => {
                    return (
                      item.pid === this.currentPart.pid &&
                      item.id !== this.currentPart.id
                    )
                  })
                  .some(item => {
                    return item.name === value
                  })
              } else {
                // 新增部门弹框条件

                flag = this.allDepart
                  .filter(item => {
                    return item.pid === this.currentPart.id
                  })
                  .some(item => {
                    return item.name === value
                  })
              }

              flag ? callback(new Error('不能与当前部门重名')) : callback()
            },
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '部门编号不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '长度为1~50个字符', trigger: 'blur' },
          {
            validator: (rules, value, callback) => {
              let flag
              if (this.formData.id) {
                // 编辑部门弹框条件
                flag = this.allDepart.some(item => {
                  return item.code === value && item.id !== this.currentPart.id
                })
              } else {
                // 新增部门弹框条件

                flag = this.allDepart.some(item => {
                  return item.code === value
                })
              }
              flag ? callback(new Error('此部门编码已存在')) : callback()
            },
            trigger: 'blur'
          }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '长度为1~300个字符', trigger: 'blur' }
        ]
      },
      // 负责人列表
      managerList: []
    }
  },
  computed: {
    mytitle() {
      return this.formData.id ? '编辑部门' : '新增子部门'
    }
  },

  methods: {
    async queryDepartment(id) {
      this.formData = await queryDepartmentApi(id)
    },
    closeDialog() {
      this.formData = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$refs.form_add.resetFields()
    },
    cancel() {
      this.$emit('update:showDialog', false)
    },
    // 新增部门
    submit() {
      this.$refs.form_add.validate(async res => {
        console.log(res)
        if (res) {
          if (this.formData.id) {
            await editDepartmentApi(this.formData)
            this.$message({
              message: '修改部门成功',
              type: 'success'
            })
            this.$emit('update:showDialog', false)
            this.$emit('refreshParts')
          } else {
            await addDepartmentApi({
              ...this.formData,
              pid: this.currentPart.id
            })
            this.$message({
              message: '新增部门成功',
              type: 'success'
            })
            this.$emit('update:showDialog', false)
            this.$emit('refreshParts')
          }
        }
      })
    },

    // 请求获取负责人列表
    async getSimpleUser() {
      const res = await getSimpleUserApi()
      this.managerList = res
    }
  },
  created() {
    this.getSimpleUser()
  }
}
</script>

<style>
</style>